<template>
  <div>
    <el-dialog
      v-el-drag-dialog
      :title="!dataForm.id ? '新增' : '修改'"
      :close-on-click-modal="false"
      :visible.sync="visible">
      <el-form :model="dataForm" :rules="dataRule" ref="dataForm" @keyup.enter.native="dataFormSubmit()" label-width="100px">
        <el-form-item label="广告标题" prop="floorName">
          <el-input v-model="dataForm.floorName"  placeholder="广告标题"></el-input>
        </el-form-item>
        <el-form-item label="开始时间" prop="time">
          <el-date-picker
            :editable="false"
            v-model="dataForm.time"
            type="daterange"
            value-format = "yyyy-MM-dd HH:mm:ss"
            range-separator="-"
            start-placeholder="活动开始时间"
            end-placeholder="活动结束时间">
          </el-date-picker>
        </el-form-item>
        <el-form-item label="广告时长" prop="advertisingDuration">
          <el-input v-model="dataForm.advertisingDuration" style="width:100px" placeholder="广告时长"></el-input><span style="font-size:18px"><b> 秒</b></span>
        </el-form-item>
        <el-form-item label="排  序" prop="seq">
          <el-input type="number" v-model="dataForm.seq" style="width:100px" placeholder="数值越大排序越靠前"></el-input>
        </el-form-item>
        <el-form-item label="是否可跳过" prop="isSkip">
          <template>
            <el-radio v-model="dataForm.isSkip" :label="0">否</el-radio>
            <el-radio v-model="dataForm.isSkip" :label="1">是</el-radio>
          </template>
        </el-form-item>
        <el-form-item label="图片名称" prop="img">
        <span><b>苹果手机尺寸</b></span><br/>
        <span>提示：请严格按照尺寸上传图片<span style="font-weight:bold;">&nbsp;上传的图片不得超过1M</span></span>
        <el-row :gutter="20">
          <el-col :span="6">
            <el-upload
            class="avatar-uploader"
            :action="this.$http.adornUrl(`/banner/uploadAdvertisingMap?token=${this.$cookie.get('token')}&type=750*1334`)"
            :show-file-list="false"
            :on-success="handleAvatarSuccess"
            :before-upload="beforeAvatarUpload">
            <img v-if="dataForm.ios750_1337" :src="dataForm.ios750_1337" class="avatar">
            <i v-else class="el-icon-plus avatar-uploader-icon"></i>
            </el-upload>
            <span>请上传750*1334px图片</span>
          </el-col>
          <el-col :span="6">
            <el-upload
            class="avatar-uploader"
            :action="this.$http.adornUrl(`/banner/uploadAdvertisingMap?token=${this.$cookie.get('token')}&type=1242*2208`)"
            :show-file-list="false"
            :on-success="handleAvatarSuccess2"
            :before-upload="beforeAvatarUpload">
            <img v-if="dataForm.ios1242_2208" :src="dataForm.ios1242_2208" class="avatar">
            <i v-else class="el-icon-plus avatar-uploader-icon"></i>
            </el-upload>
            <span>请上传1242*2208px图片</span>
          </el-col>
          <el-col :span="6">
            <el-upload
            class="avatar-uploader"
            :action="this.$http.adornUrl(`/banner/uploadAdvertisingMap?token=${this.$cookie.get('token')}&type=1125*2436`)"
            :show-file-list="false"
            :on-success="handleAvatarSuccess3"
            :before-upload="beforeAvatarUpload">
            <img v-if="dataForm.ios1125_2436" :src="dataForm.ios1125_2436" class="avatar">
            <i v-else class="el-icon-plus avatar-uploader-icon"></i>
            </el-upload>
            <span>请上传1125*2436px图片</span>
          </el-col>
        </el-row>
        <span><b>安卓手机尺寸</b></span>
        <el-row :gutter="20">
          <el-col :span="6">
            <el-upload
            class="avatar-uploader"
            :action="this.$http.adornUrl(`/banner/uploadAdvertisingMap?token=${this.$cookie.get('token')}&type=320*480`)"
            :show-file-list="false"
            :on-success="handleAvatarSuccess4"
            :before-upload="beforeAvatarUpload">
            <img v-if="dataForm.android320_480" :src="dataForm.android320_480" class="avatar">
            <i v-else class="el-icon-plus avatar-uploader-icon"></i>
            </el-upload>
            <span>请上传320*480px图片</span>
          </el-col>
          <el-col :span="6">
            <el-upload
            class="avatar-uploader"
            :action="this.$http.adornUrl(`/banner/uploadAdvertisingMap?token=${this.$cookie.get('token')}&type=480*800`)"
            :show-file-list="false"
            :on-success="handleAvatarSuccess5"
            :before-upload="beforeAvatarUpload">
            <img v-if="dataForm.android480_800" :src="dataForm.android480_800" class="avatar">
            <i v-else class="el-icon-plus avatar-uploader-icon"></i>
            </el-upload>
            <span>请上传480*800px图片</span>
          </el-col>
          <el-col :span="6">
            <el-upload
            class="avatar-uploader"
            :action="this.$http.adornUrl(`/banner/uploadAdvertisingMap?token=${this.$cookie.get('token')}&type=720*1280`)"
            :show-file-list="false"
            :on-success="handleAvatarSuccess6"
            :before-upload="beforeAvatarUpload">
            <img v-if="dataForm.android720_1280" :src="dataForm.android720_1280" class="avatar">
            <i v-else class="el-icon-plus avatar-uploader-icon"></i>
            </el-upload>
            <span>请上传720*1280px图片</span>
          </el-col>
          <el-col :span="6">
            <el-upload
            class="avatar-uploader"
            :action="this.$http.adornUrl(`/banner/uploadAdvertisingMap?token=${this.$cookie.get('token')}&type=1080*1800`)"
            :show-file-list="false"
            :on-success="handleAvatarSuccess7"
            :before-upload="beforeAvatarUpload">
            <img v-if="dataForm.android1080_1800" :src="dataForm.android1080_1800" class="avatar">
            <i v-else class="el-icon-plus avatar-uploader-icon"></i>
            </el-upload>
            <span>请上传1080*1920px图片</span>
          </el-col>
        </el-row>
        </el-form-item>
        <el-form-item label="跳转类型" prop="jumpType">
          <el-row  style="display: flex;">
            <el-select v-model="dataForm.jumpType" placeholder="分类" value="" style="margin-right: 30px">
              <el-option label="商品分类" :value="0" ></el-option>
              <el-option label="活动" :value="2" ></el-option>
              <el-option label="外链" :value="3" ></el-option>
              <el-option label="专题" :value="5" ></el-option>
              <el-option label="无跳转" :value="6" ></el-option>
              <el-option label="商品" :value="7" ></el-option>
            </el-select>
            <el-input v-model="dataForm.url" placeholder="请输入跳转类型链接"></el-input>
          </el-row>
        </el-form-item>
        <el-form-item label="备注" prop="remark">
          <el-input v-model="dataForm.remark"  type="textarea" placeholder="备注"></el-input>
        </el-form-item>
      </el-form>
      <span slot="footer" class="dialog-footer">
        <el-button @click="visible = false">取消</el-button>
        <el-button type="primary" @click="dataFormSubmit()">确定</el-button>
      </span>
    </el-dialog>

    <add-or-update v-if="addOrUpdateVisible" ref="addOrUpdate" @getGoodsList="getGoodsList"></add-or-update>
  </div>
</template>

<script>
  export default {
    data: function () {
      var seconds = (rule, value, callback) => {
        if (typeof value === 'object' || value === undefined) {
          value = rule.field
        }
        let reg = /^[0-9]\d*$/
        if (!reg.test(value)) {
          callback(this.$message.error('时长秒数应为正整数！'))
        } else {
          callback()
        }
      }
      return {
        visible: false,
        dialogImageUrl: '',
        dialogVisible: false,
        addOrUpdateVisible: false,
        goodsList: [],
        goodsInfoList: [],
        dataForm: {
          id: 0,
          remark: '',
          android320_480: '',
          android480_800: '',
          android720_1280: '',
          android1080_1800: '',
          ios750_1337: '',
          ios1242_2208: '',
          ios1125_2436: '',
          seq: 0,
          time: new Date(),
          isSkip: '',
          advertisingDuration: '',
          floorName: '',
          url: '',
          jumpType: ''
        },
        dataRule: {
          floorName: [
            { required: true, message: '广告标题不能为空', trigger: 'blur' }
          ],
          time: [
            { required: true, message: '时间不能为空', trigger: 'blur' }
          ],
          advertisingDuration: [
            { validator: seconds, required: true, trigger: 'blur' }
          ],
          isSkip: [
            { required: true, message: '是否跳过不能为空', trigger: 'blur' }
          ]
        }
      }
    },
    methods: {
      handleAvatarSuccess (res, file) {
        this.dataForm.ios750_1337 = res.data
      },
      handleAvatarSuccess2 (res, file) {
        this.dataForm.ios1242_2208 = res.data
      },
      handleAvatarSuccess3 (res, file) {
        this.dataForm.ios1125_2436 = res.data
      },
      handleAvatarSuccess4 (res, file) {
        this.dataForm.android320_480 = res.data
      },
      handleAvatarSuccess5 (res, file) {
        this.dataForm.android480_800 = res.data
      },
      handleAvatarSuccess6 (res, file) {
        this.dataForm.android720_1280 = res.data
      },
      handleAvatarSuccess7 (res, file) {
        this.dataForm.android1080_1800 = res.data
      },
      beforeAvatarUpload (file) {
        console.log(file)
        const isJPG = file.type === 'image/jpeg' || 'image/png'
        // const isLt2M = file.size / 1024 / 1024 < 2;

        if (!isJPG) {
          this.$message.error('上传头像图片只能是 JPG 格式!')
        }
        // if (!isLt2M) {
        //   this.$message.error('上传头像图片大小不能超过 2MB!');
        // }
        // return isJPG && isLt2M;
        return isJPG
      },
      init (id) {
        this.dataForm.remark = ''
        this.dataForm.android320_480 = ''
        this.dataForm.android480_800 = ''
        this.dataForm.android720_1280 = ''
        this.dataForm.android1080_1800 = ''
        this.dataForm.ios750_1337 = ''
        this.dataForm.ios1242_2208 = ''
        this.dataForm.ios1125_2436 = ''
        this.dataForm.seq = 0
        this.dataForm.time = new Date()
        this.dataForm.isSkip = ''
        this.dataForm.advertisingDuration = ''
        this.dataForm.floorName = ''
        this.dataForm.url = ''
        this.dataForm.jumpType = ''
        this.dataForm.id = id || 0
        this.visible = true
        this.$nextTick(() => {
          if (this.dataForm.id) {
            this.$http({
              url: this.$http.adornUrl(`/banner/getStartAdvertisingById/${this.dataForm.id}`),
              method: 'get',
              params: this.$http.adornParams()
            }).then(({data}) => {
              if (data && data.code === 0) {
                this.dataForm.jumpType = data.data.jumpType
                this.dataForm.floorName = data.data.floorName
                this.dataForm.url = data.data.url
                this.dataForm.seq = data.data.seq
                this.dataForm.time = [data.data.startTime, data.data.endTime]
                this.dataForm.isSkip = data.data.isSkip
                this.dataForm.advertisingDuration = data.data.advertisingDuration
                this.dataForm.remark = data.data.remark
                if (data.data.urlData !== '') {
                  let img = data.data.urlData.split(',')
                  for (var i = 0; i < img.length; i++) {
                    if (img[i].indexOf('750*1334') > 1) {
                      this.dataForm.ios750_1337 = img[i]
                    } else if (img[i].indexOf('1242*2208') > 1) {
                      this.dataForm.ios1242_2208 = img[i]
                    } else if (img[i].indexOf('1125*2436') > 1) {
                      this.dataForm.ios1125_2436 = img[i]
                    } else if (img[i].indexOf('320*480') > 1) {
                      this.dataForm.android320_480 = img[i]
                    } else if (img[i].indexOf('480*800') > 1) {
                      this.dataForm.android480_800 = img[i]
                    } else if (img[i].indexOf('720*1280') > 1) {
                      this.dataForm.android720_1280 = img[i]
                    } else if (img[i].indexOf('1080*1800') > 1) {
                      this.dataForm.android1080_1800 = img[i]
                    }
                  }
                }
              }
            })
          }
        })
      },
      // 表单提交
      dataFormSubmit: function () {
        this.$refs['dataForm'].validate((valid) => {
          if (valid) {
            if (this.dataForm.ios750_1337 === '') {
              this.$message.error('请上传苹果750*1337尺寸图片！')
              return
            }
            if (this.dataForm.ios1242_2208 === '') {
              this.$message.error('请上传苹果1242*2208尺寸图片！')
              return
            }
            if (this.dataForm.ios1125_2436 === '') {
              this.$message.error('请上传苹果1125*2436尺寸图片！')
              return
            }
            if (this.dataForm.android320_480 === '') {
              this.$message.error('请上传安卓320*480尺寸图片！')
              return
            }
            if (this.dataForm.android480_800 === '') {
              this.$message.error('请上传安卓480*800尺寸图片！')
              return
            }
            if (this.dataForm.android720_1280 === '') {
              this.$message.error('请上传安卓720*1280尺寸图片！')
              return
            }
            if (this.dataForm.android1080_1800 === '') {
              this.$message.error('请上传安卓1080*1800尺寸图片！')
              return
            }
            this.$http({
              url: this.$http.adornUrl(`/banner/${!this.dataForm.id ? 'addStartAdvertising' : 'modifyStartAdvertisingById'}`),
              method: 'post',
              data: this.$http.adornData({
                'id': this.dataForm.id || undefined,
                'floorName': this.dataForm.floorName,
                'seq': this.dataForm.seq,
                'urlData': this.dataForm.ios750_1337 + ',' + this.dataForm.ios1242_2208 + ',' + this.dataForm.ios1125_2436 + ',' + this.dataForm.android320_480 + ',' + this.dataForm.android480_800 + ',' + this.dataForm.android720_1280 + ',' + this.dataForm.android1080_1800,
                'remark': this.dataForm.remark,
                'isSkip': this.dataForm.isSkip,
                'advertisingDuration': this.dataForm.advertisingDuration,
                'url': this.dataForm.url,
                'startTime': this.dataForm.time[0],
                'endTime': this.dataForm.time[1],
                'jumpType': this.dataForm.jumpType
              })
            }).then(({data}) => {
              if (data && data.code === 0) {
                this.$message({
                  message: '操作成功',
                  type: 'success',
                  duration: 1500,
                  onClose: () => {
                    this.visible = false
                    this.$emit('refreshDataList')
                  }
                })
              } else {
                this.$message.error(data.msg)
              }
            })
          }
        })
      }
    }
  }
</script>
<style>
  .avatar-uploader .el-upload {
    border: 1px dashed #d9d9d9;
    border-radius: 6px;
    cursor: pointer;
    position: relative;
    overflow: hidden;
  }
  .avatar-uploader .el-upload:hover {
    border-color: #409EFF;
  }
  .avatar-uploader-icon {
    font-size: 28px;
    color: #8c939d;
    width: 128px;
    height: 128px;
    line-height: 128px;
    text-align: center;
  }
  .avatar {
    width: 128px;
    height: 128px;
    display: block;
  }
</style>
